{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5908c744",
   "metadata": {},
   "source": [
    "# OCR(光学字符识别)\n",
    "* 实践并观察res结果\n",
    "> 1. 通用的场景文字识别\n",
    ">> 1. 通用文字识别\n",
    ">> 2. 网络图片文字识别\n",
    ">> 3. 手写文字识别\n",
    ">> 4. 二维码识别（二维码转文本）\n",
    "> 2. 卡证文字识别\n",
    ">> 1. 身份证识别\n",
    ">> 2. 银行卡\n",
    "> 3. 财务票据文字识别\n",
    ">> 1. 增值税发票\n",
    ">> 2. 火车票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "866412b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.2413af98474d98f4a43c8f377a21292f.315360000.1965093664.282335-25948930', 'expires_in': 2592000, 'session_key': '9mzdX+m4wvTgT8kes2PdtIy+5jX9PQCm0laPuCkttIL+oZfV4olxGCieKXhT7hNRBM608ManF/rLNGimNNGXVeAgYsfUeg==', 'access_token': '24.d39d01f1615815153278bdf9394cc882.2592000.1652325664.282335-25948930', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '1af835373a6b37646122f74a7ea923ab'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'LZIFxQhy1xHEwZDc3O7tzXpF',\n",
    "    'client_secret':'MqbRVVFDXMAeyXyy57xX48AkU7UD41kP'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d1a9ed5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.d39d01f1615815153278bdf9394cc882.2592000.1652325664.282335-25948930'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "haixin_AT = response.json()['access_token']\n",
    "haixin_AT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "754ca064",
   "metadata": {},
   "source": [
    "# 通用的场景文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83654819",
   "metadata": {},
   "source": [
    "## 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "375e611f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '论诗'}, {'words': '清·赵翼'}, {'words': '李杜诗篇万口传，'}, {'words': '至今已觉不新鲜。'}, {'words': '江山代有才人出，'}, {'words': '各领风骚数百年。'}], 'words_result_num': 6, 'log_id': 1513718847026779256}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('shi.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1462677b",
   "metadata": {},
   "source": [
    "## 网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0269cb9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '禧瑞天'}, {'words': '首创置业'}, {'words': '星耀而出'}, {'words': '一生之选'}, {'words': '舍我其谁'}, {'words': '禧瑞天著'}, {'words': '座城市的热销传奇'}, {'words': '「五环都心·国贸线·全景洋房」'}, {'words': '咨询热线'}, {'words': '66077077'}, {'words': '项目地址'}, {'words': '中国·亦庄·京东总部向东约1000米(亦庄线次渠南站西'}], 'words_result_num': 12, 'log_id': 1513719530076233904}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('wltp.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34473d32",
   "metadata": {},
   "source": [
    "## 手写文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ee61acda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'location': {'top': 102, 'left': 110, 'width': 159, 'height': 57}, 'words': \"Morandi's Bottles \"}, {'location': {'top': 168, 'left': 108, 'width': 338, 'height': 135}, 'words': '愿所有事情只要你觉得'}, {'location': {'top': 190, 'left': 408, 'width': 64, 'height': 28}, 'words': 'The painter '}, {'location': {'top': 232, 'left': 85, 'width': 308, 'height': 123}, 'words': '值得就好悲喜自渡'}, {'location': {'top': 292, 'left': 52, 'width': 166, 'height': 79}, 'words': '他人难悟'}], 'words_result_num': 5, 'log_id': 1513720178549075672}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "手写文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('sx.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99184bf8",
   "metadata": {},
   "source": [
    "## 二维码文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9e6b7c2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['你好，欢迎来到API的世界！'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513721299026490017}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('ewm.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8768309e",
   "metadata": {},
   "source": [
    "# 卡片文本识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "792c0c87",
   "metadata": {},
   "source": [
    "## 身份证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "32d3bf02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 46, 'left': 107, 'width': 28, 'height': 15}, 'words': '许哲'}, '民族': {'location': {'top': 73, 'left': 168, 'width': 11, 'height': 13}, 'words': '汉'}, '住址': {'location': {'top': 122, 'left': 103, 'width': 132, 'height': 30}, 'words': '上海市浦东新区张江高科郭守敬路356号'}, '公民身份号码': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '出生': {'location': {'top': 96, 'left': 102, 'width': 106, 'height': 14}, 'words': '19910730'}, '性别': {'location': {'top': 73, 'left': 106, 'width': 12, 'height': 13}, 'words': '男'}}, 'words_result_num': 6, 'idcard_number_type': 0, 'image_status': 'unknown', 'log_id': 1513722557492651481}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('sfz.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img,\n",
    "    'id_card_side':\"front\"\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38cc7d2e",
   "metadata": {},
   "source": [
    "## 银行卡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "61e69db7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'valid_date': '88/88', 'bank_card_number': '6225 7688 8888 8888', 'bank_name': '招商银行', 'bank_card_type': 2, 'holder_name': 'SIXTANG RI KUI'}, 'direction': 0, 'log_id': 1513723197571627809}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "银行卡文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('yhk.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "829e74df",
   "metadata": {},
   "source": [
    "# 财务票据文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ffc5cd",
   "metadata": {},
   "source": [
    "## 增值税发票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e8e28fcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '叁拾伍圆整', 'InvoiceNumConfirm': '20872218', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '33.9805825242'}], 'NoteDrawer': '管理员', 'SellerAddress': '北京市海淀区成府路蓝旗营1至5号楼C区地上一层113室010-62753839', 'CommodityNum': [{'row': '1', 'word': '1'}], 'SellerRegisterNum': '91110108599696488J', 'MachineCode': '499926851509', 'Remarks': '校验码08789940450588603872', 'SellerBank': '北京农村商业银行科技园支行0419000103000005121', 'CommodityTaxRate': [{'row': '1', 'word': '3%'}], 'TotalTax': '1.02', 'InvoiceCodeConfirm': '1100172320', 'CheckCode': '08789940450588603872', 'InvoiceCode': '1100172320', 'InvoiceDate': '2018年03月21日', 'PurchaserRegisterNum': '', 'InvoiceTypeOrg': '北京增值税普通发票', 'OnlinePay': '', 'Password': '03*22+8<*1*212--189/*216540>538+55+81-95/77777+<*42/4<>+/4*133+/8/407<35>470+1763+064>1/+4<*/5012*2806<->0808952', 'Agent': '否', 'AmountInFiguers': '35.00', 'PurchaserBank': '', 'Checker': '管理员', 'City': '', 'TotalAmount': '33.98', 'CommodityAmount': [{'row': '1', 'word': '33.98'}], 'PurchaserName': '昌无', 'CommodityType': [], 'Province': '北京', 'InvoiceType': '普通发票', 'SheetNum': '第二联：发票联', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '1.02'}], 'CommodityPlateNum': [], 'CommodityUnit': [], 'Payee': '', 'CommodityName': [{'row': '1', 'word': '*设计服务*打印费'}], 'SellerName': '北京城环嘉苗科技有限公司', 'InvoiceNum': '20872218'}, 'log_id': 1513724632466893744}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('zzs.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3db29c6",
   "metadata": {},
   "source": [
    "## 火车票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ed881282",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 13, 'words_result': {'id_num': '3424231998****1540', 'serial_number': '14846301011007R093443', 'ticket_num': 'R093443', 'seat_num': '14车015号上铺', 'time': '16:05', 'date': '2019年10月06日', 'name': '裴丽丽', 'destination_station': '北京站', 'seat_category': '新空调硬卧', 'starting_station': '银川站', 'sales_station': '银川', 'train_num': 'K1178', 'ticket_rates': '￥280.5元'}, 'direction': 0, 'log_id': 1513725983268366080}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "火车票文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/train_ticket\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1. 图片文件准备\n",
    "f = open('hcp.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载参数准备\n",
    "payload = {\n",
    "    'access_token':haixin_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
